package com.lx.idea.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lx.idea.aspect.LogAnnotation;
import com.lx.idea.common.config.ElasticsearchConfig;
import com.lx.idea.common.dto.ResponseCode;
import com.lx.idea.common.dto.ResponseModel;
import com.lx.idea.model.AnalysisTb;
import com.lx.idea.service.AnalysisServiceImpl;
import com.lx.idea.service.NodeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author ybb
 * @Description
 * @Date 2021/8/30 14:36
 */
@RestController
@RequestMapping("/ma-api")
@Api(tags = "MA-标识管理接口")
public class NodeController {

    @Autowired
    NodeServiceImpl nodeService;

    @Autowired
    AnalysisServiceImpl analysisService;

    @Autowired
    ElasticsearchConfig elasticsearchConfig;


    /**
     * 外部调用标识获取
     * @param analysisUrl
     * @param request
     * @return
     */
    @GetMapping("/analysis/ma")
    @LogAnnotation
    @ApiOperation(value = "MA-解析标识获取数据", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    public ResponseModel analysis(@RequestParam String analysisUrl,
                                  HttpServletRequest request){
        Object obj = nodeService.getDocument(request,analysisUrl);
        if (null == obj){
            return ResponseModel.error(ResponseCode.PREFIX_PARSE_ERROR,ResponseCode.PREFIX_PARSE_ERROR.getMessage());
        }
        return ResponseModel.success(obj);
    }

    /**
     * 内部https访问
     * @param analysisUrl
     * @param request
     * @return
     */
    @GetMapping("/internal/analysis/ma")
    @LogAnnotation
    @ApiOperation(value = "MA-内部-解析标识获取数据", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    public ResponseModel analysisInternal(@RequestParam String analysisUrl,
                                  HttpServletRequest request){
        System.out.println("请求尽量了");
        Object obj = nodeService.getDocument(request,analysisUrl);
        if (null == obj){
            return ResponseModel.error(ResponseCode.PREFIX_PARSE_ERROR,ResponseCode.PREFIX_PARSE_ERROR.getMessage());
        }
        return ResponseModel.success(obj);
    }

    @PostMapping("/analysis/save")
    @ResponseBody
    @ApiOperation(value = "MA-新增/修改标识", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    public ResponseModel analysisSave(@RequestBody AnalysisTb analysisTb){
        if (null == analysisTb){
            return ResponseModel.error(ResponseCode.INVALID_PARAMETER,ResponseCode.INVALID_PARAMETER.getMessage());
        }
        AnalysisTb res = analysisService.save(analysisTb);
        if (null == res){
            return ResponseModel.error(ResponseCode.PREFIX_VALID_ERROR,ResponseCode.PREFIX_VALID_ERROR.getMessage());
        }
        return ResponseModel.success(res);
    }

    @DeleteMapping("/analysis/del/")
    @ApiOperation(value = "MA-删除标识", httpMethod = "DELETE", produces = "application/json", response = ResponseModel.class)
    public ResponseModel analysisDel(@RequestParam String analysisPath){
        int count = analysisService.del(analysisPath);
        return count >0 ?ResponseModel.success():ResponseModel.error(ResponseCode.SYS_ERROR,ResponseCode.SYS_ERROR.getMessage());
    }

    @GetMapping("/analysis/nodeInfo")
    @ApiOperation(value = "MA-获取标识所在注册节点信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    public ResponseModel nodeInfo(@RequestParam String analysisUrl, HttpServletRequest request){
        Object obj = nodeService.nodeInfo(analysisUrl,request);
        if (null == obj){
            return ResponseModel.error(ResponseCode.PREFIX_PARSE_ERROR,ResponseCode.PREFIX_PARSE_ERROR.getMessage());
        }
        return ResponseModel.success(obj);

    }
    @GetMapping("/analysis/nextNode")
    @ApiOperation(value = "MA-获取下级节点信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    public ResponseModel nextNode(@RequestParam String analysisUrl){
        Object obj = nodeService.nextNode(analysisUrl);
        if (null == obj){
            return ResponseModel.error(ResponseCode.PREFIX_PARSE_ERROR,ResponseCode.PREFIX_PARSE_ERROR.getMessage());
        }
        return ResponseModel.success(obj);

    }


    @GetMapping("/internal/analysis/nodeInfo")
    @ApiOperation(value = "MA-内部-获取标识所在注册节点信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    public ResponseModel nodeInfoInternal(@RequestParam String analysisUrl, HttpServletRequest request){
        Object obj = nodeService.nodeInfo(analysisUrl,request);
        if (null == obj){
            return ResponseModel.error(ResponseCode.PREFIX_PARSE_ERROR,ResponseCode.PREFIX_PARSE_ERROR.getMessage());
        }
        return ResponseModel.success(obj);

    }
    @GetMapping("/internal/analysis/nextNode")
    @ApiOperation(value = "MA-内部-获取下级节点信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    public ResponseModel nextNodeInternal(@RequestParam String analysisUrl){
        Object obj = nodeService.nextNode(analysisUrl);
        if (null == obj){
            return ResponseModel.error(ResponseCode.PREFIX_PARSE_ERROR,ResponseCode.PREFIX_PARSE_ERROR.getMessage());
        }
        return ResponseModel.success(obj);

    }
    @GetMapping("/analysis/list")
    @LogAnnotation
    @ApiOperation(value = "MA-获取当前节点的所有标识", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    public ResponseModel analysisList(@RequestParam Integer pageNo,@RequestParam Integer pageSize,
                                  HttpServletRequest request){
        PageHelper.startPage(pageNo, pageSize);
        List<AnalysisTb> list = analysisService.getList();
        if (null == list){
            return ResponseModel.error(ResponseCode.PREFIX_PARSE_ERROR,ResponseCode.PREFIX_PARSE_ERROR.getMessage());
        }
        PageInfo<AnalysisTb> pageInfo = new PageInfo<AnalysisTb>(list);
        return ResponseModel.success(pageInfo);
    }


}
